﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;

namespace Wpf_Examples.ViewModels
{
    public class ListBoxMenuViewModel:ObservableObject
    {
        List<object> views;

        #region 属性

        private object bodyView;
        public object BodyView
        {
            get { return bodyView; }
            set { SetProperty(ref bodyView, value); }
        }

        #endregion


        #region 命令
        public RelayCommand<string> ButtonClickCmd { get; set; }
        #endregion

        public ListBoxMenuViewModel()
        {
            ButtonClickCmd = new RelayCommand<string>(FunMenu);

            views = new List<object>();
            BodyView = new Views.Configs.SystemConfig();
            views.Add(BodyView);
        }

        #region 菜单按钮方法
        private void FunMenu(string obj)
        {
            switch (obj)
            {
                case "系统配置":
                    BodyViewChanged(this, typeof(Views.Configs.SystemConfig));
                    break;
                case "数据查询":
                    BodyViewChanged(this, typeof(Views.Configs.DataResearchConfig));
                    break;
                case "报警配置":
                    BodyViewChanged(this, typeof(Views.Configs.AlarmConfig));
                    break;
            }
        }

        /// <summary>
        /// 主界面视图切换
        /// </summary>
        void BodyViewChanged(object recipient, Type view)
        {
            var currentView = views.FirstOrDefault(p => p.GetType() == view);
            if (currentView == null)
            {
                BodyView = Activator.CreateInstance(view) as UserControl;
                views.Clear();//清理旧页面，每次保证页面重新加载，不清理则可以使用缓存，提高软件流畅度
                views.Add(BodyView);
            }
            else
            {
                BodyView = currentView;
            }
        }

        #endregion
    }
}
